<?php

/**
 * Product form base class.
 *
 * @method Product getObject() Returns the current form's model object
 *
 * @package    erpcrm
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 24171 2009-11-19 16:37:50Z Kris.Wallsmith $
 */
abstract class BaseProductForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'iden'                          => new sfWidgetFormInputHidden(),
      'product_name'                  => new sfWidgetFormTextarea(),
      'product_status'                => new sfWidgetFormInputText(),
      'product_type'                  => new sfWidgetFormInputText(),
      'product_size'                  => new sfWidgetFormInputText(),
      'color_code'                    => new sfWidgetFormInputText(),
      'color_name'                    => new sfWidgetFormInputText(),
      'article_code'                  => new sfWidgetFormInputText(),
      'product_unit'                  => new sfWidgetFormInputText(),
      'product_package_unit'          => new sfWidgetFormInputText(),
      'reorder_quantity'              => new sfWidgetFormInputText(),
      'remarks'                       => new sfWidgetFormTextarea(),
      'supplier_iden'                 => new sfWidgetFormPropelChoice(array('model' => 'AccountSupplier', 'add_empty' => false)),
      'product_category_product_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'ProductCategory')),
    ));

    $this->setValidators(array(
      'iden'                          => new sfValidatorPropelChoice(array('model' => 'Product', 'column' => 'iden', 'required' => false)),
      'product_name'                  => new sfValidatorString(),
      'product_status'                => new sfValidatorString(array('max_length' => 50)),
      'product_type'                  => new sfValidatorString(array('max_length' => 50)),
      'product_size'                  => new sfValidatorString(array('max_length' => 50)),
      'color_code'                    => new sfValidatorString(array('max_length' => 50)),
      'color_name'                    => new sfValidatorString(array('max_length' => 50)),
      'article_code'                  => new sfValidatorString(array('max_length' => 100)),
      'product_unit'                  => new sfValidatorString(array('max_length' => 50)),
      'product_package_unit'          => new sfValidatorString(array('max_length' => 50)),
      'reorder_quantity'              => new sfValidatorNumber(),
      'remarks'                       => new sfValidatorString(),
      'supplier_iden'                 => new sfValidatorPropelChoice(array('model' => 'AccountSupplier', 'column' => 'contact_iden')),
      'product_category_product_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'ProductCategory', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('product[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Product';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['product_category_product_list']))
    {
      $values = array();
      foreach ($this->object->getProductCategoryProducts() as $obj)
      {
        $values[] = $obj->getCategoryIden();
      }

      $this->setDefault('product_category_product_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveProductCategoryProductList($con);
  }

  public function saveProductCategoryProductList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['product_category_product_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(ProductCategoryProductPeer::PRODUCT_IDEN, $this->object->getPrimaryKey());
    ProductCategoryProductPeer::doDelete($c, $con);

    $values = $this->getValue('product_category_product_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new ProductCategoryProduct();
        $obj->setProductIden($this->object->getPrimaryKey());
        $obj->setCategoryIden($value);
        $obj->save();
      }
    }
  }

}
